﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2.Controls
{
    public partial class DatabaseModificationsUserControl : UserControl
    {
        public DatabaseModificationsUserControl()
        {
            InitializeComponent();
            this.FillServerComboBox();
        }

        private void FillServerComboBox()
        {
            this.serverConnectionStringList = sqlConnection.GetServerComboBoxFromFile("serverConnection.txt");

            foreach (string serverConnectionStringListItem in serverConnectionStringList)
            {
                serverCSComboBox.Items.Add(serverConnectionStringListItem);
            }
        }

        SQLConnection sqlConnection = new SQLConnection();
        SQLModifications databaseModifications = new SQLModifications();
        WebConfigParser webConfigParser = new WebConfigParser();
        List<string> serverConnectionStringList = new List<string>();
        List<string> databasesList = new List<string>();
        List<Guid> guidList = new List<Guid>();// tu jest list GUIDÓW - nie bede używać
        List<string> userNameList = new List<string>();// tu jest list userów co będzie wykorzystane w GUIDACH
        List<int> clientList = new List<int>(); //tu jest lista klientów
        List<int> fatureGruopsIDList = new List<int>();
        List<string> listOfTablesToModify = new List<string>();


        private void editServerCSButton_Click(object sender, EventArgs e)
        {
        }

        private void setDafaultButton_Click(object sender, EventArgs e)
        {
            if (guidsComboBox.Text != null && clientsComboBox.Text != null)
            {
               // databaseModyfications.SetDefaultClientUser(Guid.Parse(guidsComboBox.Text), int.Parse(clientsComboBox.Text));
                databaseModifications.SetDefaultClientUser(guidsComboBox.Text, int.Parse(clientsComboBox.Text));
            }
        }

        private void addFGuidsToTabelButton_Click(object sender, EventArgs e)
        {
            if (listOfTablesToModifyComboBox.Text == "ClientUser")
            {
                databaseModifications.InsertIntoClientUser();
            }
            else if (listOfTablesToModifyComboBox.Text == "FeatureGroupUser")
            {
                databaseModifications.InsertIntoFeatureGroupUser();
            }
            else if (listOfTablesToModifyComboBox.Text == "DeveloperAuthentication")
            {
            }
        }

        private void serverCSComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            sqlConnection.serverConnectionString = serverCSComboBox.SelectedItem.ToString();
            databaseModifications.serverConnectionString = serverCSComboBox.SelectedItem.ToString(); // to jest bardzo wazne
            this.databasesList = sqlConnection.GetDatabasesComboBox();
            this.ClearComboBoxes(1);
            this.ClearComboBoxes(0);
            replaceButton.Enabled = false;

            foreach (string databasesListItem in databasesList)
            {
                if (databasesListItem.Contains("Textures"))
                {
                    databasesComboBox.Items.Add(databasesListItem);
                }
            }

            foreach (string databasesListItem in databasesList)
            {
                if (databasesListItem.Contains("OnlineServices"))
                {
                    osDatabasesNameComboBox.Items.Add(databasesListItem);
                }
            }
        }

        private void databasesComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            if (databasesComboBox.SelectedIndex != -1)
            {
                sqlConnection.databaseName = databasesComboBox.SelectedItem.ToString();
                guidsComboBox.Items.Clear();
                clientsComboBox.Items.Clear();
                this.ClearComboBoxes(1);
                string databaseName = databasesComboBox.SelectedItem.ToString(); //
                databaseModifications.databaseName = databaseName; //przekazuje wybrana nazwe bazy do klasy databasemodyfications
                this.guidList = databaseModifications.GetGUIDSList();
                this.userNameList = databaseModifications.GetUsersList();
                this.clientList = databaseModifications.GetClientsList();
                this.fatureGruopsIDList = databaseModifications.GetFeatureGroupUserIDList();// tu pobiera złe dane!!!!!
                this.listOfTablesToModify = databaseModifications.GetListOfTablesToModify();
                replaceButton.Enabled = true;

                foreach (string userNameListItem in userNameList) //wypełniam comboboxa guidami
                {
                    guidsComboBox.Items.Add(userNameListItem).ToString();
                }

                foreach (int clientListItem in clientList)// wypełniam comboboxa klientami
                {
                    clientsComboBox.Items.Add(clientListItem).ToString();
                }

                foreach (string tabelToModifyListItem in listOfTablesToModify)// wypełniam comboboxa tabelami
                {
                    listOfTablesToModifyComboBox.Items.Add(tabelToModifyListItem).ToString();
                }

                if (guidsComboBox.Items.Count != 0)
                {
                    assignDefaultClientGroupBox.Show();
                }

                if (listOfTablesToModifyComboBox.Items.Count != 0)
                {
                    addGUIDsToTablesGroupBox.Show();
                }

                if (osDatabasesNameComboBox.Items.Count != 0)
                {
                    usersViewModyfication.Show();
                }
            }
        }

        private void osDatabasesNameComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            string onlineServicesDatabaseName = osDatabasesNameComboBox.SelectedItem.ToString(); //
            databaseModifications.onlineServicesDatabaseName = onlineServicesDatabaseName;
        }

        private void ClearComboBoxes(int grade)
        {
            if (grade == 0)
            {
                databasesComboBox.Items.Clear();
                databasesComboBox.Text = string.Empty;
                osDatabasesNameComboBox.Text = string.Empty;
                osDatabasesNameComboBox.Items.Clear();
            }
            else if (grade == 1)
            {
                guidsComboBox.Items.Clear();
                guidsComboBox.Text = string.Empty;
                clientsComboBox.Items.Clear();
                clientsComboBox.Text = string.Empty;
                listOfTablesToModifyComboBox.Items.Clear();
                listOfTablesToModifyComboBox.Text = string.Empty;
                //osDatabasesNameComboBox.Text = string.Empty;
                //osDatabasesNameComboBox.Items.Clear();
            }
            else if (grade == 3)
            {
                serverCSComboBox.Items.Clear();
                serverCSComboBox.Text = string.Empty;
            }
        }

        private void DatabaseModificationsUserControl_VisibleChanged(object sender, EventArgs e)
        {
            this.ClearComboBoxes(0);
            this.ClearComboBoxes(1);
            this.ClearComboBoxes(3);
            this.FillServerComboBox();
        }

        private void replaceButton_Click(object sender, EventArgs e)
        {
            this.databaseModifications.ChangeUserView();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.sqlConnection.model();
        }
    }
}
